<?php

/**
 * “我的”栏目下数据逻辑封装, order, red_packet
 */

namespace Logic;

class ORC {

    /**
     * 查询订单信息
     */
    public static function orderGet($where = "1=1", $page = 1, $order = '') {
        $return = ['data_list' => [], 'total_page' => 0];

        $limit = 20;
        $offset = ($page - 1) * $limit;
        $order = $order ? $order : '`id` desc';

        $tc = \Db::instance()->count("select count(1) from `myorder` where $where");
        $total_page = ceil($tc / $limit);

        $sql = "select * from `myorder` where $where order by $order limit $limit offset $offset";
        $mylist = \Db::instance()->getList($sql);
        if (!$mylist) {
            // 没有查询到记录返回空
            return $return;
        }

        $order_ids = array_column($mylist, 'order_id');
        $orderlist = static::getOrderList($order_ids);

        // 格式化数据
        $resp = static::formatMO($mylist, $orderlist);
        $return = ['total_page' => $total_page, 'data_list' => $resp];

        return $return;
    }

    /**
     * 查询订单信息
     */
    private static function getOrderList($order_ids = []) {
        $where = "`order_id` in('" . implode("','", $order_ids) . "')";
        $sql = "select `order_id`,`create_time`,`product_id`,`title`,`payment`,`pre_commission`,`pre_red_packet`,`account_payment`,"
                . "`account_commission`,`account_time`,`red_packet`,`order_status` from `orderlist` where $where";
        $rs = \Db::instance()->getList($sql);
        if (!$rs) {
            return [];
        }

        $return = [];
        foreach ($rs as $r) {
            $return[$r['order_id']][] = $r;
        }

        return $return;
    }

    /**
     * 格式化myorder和对应的orderlist数据
     */
    private static function formatMO($mylist = [], $orderlist = []) {
        $return = [];

        $ulist = static::getWxUser($mylist);

        foreach ($mylist as $k => $my) {
            $return[$k] = $my;

            $openid = $my['openid'];
            $return[$k]['userid'] = $my['user_id'];
            $return[$k]['username'] = $my['user_id'];
            if ($openid && isset($ulist[$openid])) {
                $return[$k]['username'] = $ulist[$openid]['username'];
            }

            $status = $my['status'];
            $return[$k]['statusStr'] = isset(\Config\System::$site['orderStatus'][$status]) ? \Config\System::$site['orderStatus'][$status] : $status;
            $return[$k]['total_packet'] = 0;

            $order_id = $my['order_id'];
            $itemList = isset($orderlist[$order_id]) ? $orderlist[$order_id] : [];
            if (!$itemList) {
                $return[$k]['itemList'] = [];
                continue;
            }

            $itemArr = [];
            $total_packet = 0;
            foreach ($itemList as $item) {
                $order_status = $item['order_status'];
                $tmp = [
                    'product_id' => $item['product_id'],
                    'create_time' => $item['create_time'],
                    'title' => $item['title'],
                    'payment' => 0,
                    'commission' => 0,
                    'order_status' => $order_status,
                    'account_time' => 0,
                    'red_packet' => 0,
                    'red_packet_str' => '',
                ];

                if ($order_status == '订单付款') {
                    $tmp['payment'] = $item['payment'];
                    $tmp['commission'] = $item['pre_commission'];
                    $tmp['red_packet'] = $item['pre_red_packet'];
                    $tmp['red_packet_str'] = "预领红包" . $item['pre_red_packet'] . '元';
                }

                if ($order_status == '订单结算') {
                    $tmp['payment'] = $item['account_payment'];
                    $tmp['commission'] = $item['account_commission'];
                    $tmp['red_packet'] = $item['red_packet'];
                    $tmp['red_packet_str'] = "可领红包" . $item['red_packet'] . '元';
                    $tmp['account_time'] = $item['account_time'];
                    $total_packet += $item['red_packet'];
                }

                $itemArr[] = $tmp;
            }

            $return[$k]['itemList'] = $itemArr;
            $return[$k]['total_packet'] = $total_packet;
        }

        return $return;
    }

    /**
     * my order对应wx_user信息.
     */
    private static function getWxUser($mylist = []) {
        $return = [];
        $openids = [];

        $tmp = array_column($mylist, 'openid');
        foreach ($tmp as $t) {
            if ($t) {
                $openids[] = $t;
            }
        }

        if (!$openids) {
            return $return;
        }


        $sql = "select `id`,`openid`,`weixinid`,`nickname` from `wx_user` where `openid` in('" . implode("','", $openids) . "')";
        $ulist = \Db::instance()->getList($sql);

        foreach ($ulist as $u) {
            $id = $u['id'];
            $openid = $u['openid'];
            $weixinid = $u['weixinid'];
            $nickname = $u['nickname'];

            if ($nickname) {
                $return[$openid]['username'] = $nickname;
                continue;
            }

            if ($weixinid) {
                $return[$openid]['username'] = $weixinid;
                continue;
            }

            $return[$openid]['username'] = 'uid' . $id;
        }

        return $return;
    }

}
